#include "Board.h"
Board::Board(int a_x, int a_y)
{
	m_x = a_x;
	m_y = a_y;
}

bool Board::loadMap(char* a_filename)
{
	int x = 0;
	int y = 0;
	char letter;
	ifstream infile(a_filename);

	if(!infile.is_open())
	{
		printf("File could not be opened!\n");
		return false;
	}

	// Get the width and the height
	infile >> m_width >> m_height;

	// Allocate the array
	m_map = new char*[m_height];
	for(int i = 0; i < m_height; i++)
	{
		m_map[i] = new char[m_width];
	}

	// Fill the array
	while(1) // Loop forever
	{
		letter = infile.get();
		
		// Give up if you're at EOF
		if(letter == EOF)
			break;
		
		if(letter != '\n' && letter != '\r')
		{
			m_map[y][x] = letter;
			x++;
			if(x >= m_width)
			{
				y++;
				x = 0;

				if(y >= m_height)
					break;
			}
		}
	}

	return true;
}

bool Board::loadSpaces(char* a_filename, PropertyList* a_properties)
{
	ifstream infile(a_filename);
	Property* propCard;
	Space* newSpace;
	int price;
	char* name;
	char dummy[256];
	char token;		

	if(!infile.is_open())
	{
		printf("File could not be opened!\n");
		return false;
	}

	while(!infile.eof())
	{
		// Read one character
		token = infile.peek();
		
		// Ignore comments and empty lines
		if(token == '#' || token == '\n' || token == '\r')
		{ 
			infile.getline(dummy, 255, '\n');
		}
		else // Found a good token!
		{
			// Clean out our temporary variables.
			name = new char[256];
			price = 0;

			// Load the Name
			infile.getline(name, 255, '\n');
				
			// Find the property card that matches this space
			propCard = a_properties->findProperty(name);

			// Load the price
			infile >> price;

			// Create the card and push it back						
			newSpace = new Space(propCard, name, price);

			m_spaces.push_back(*newSpace);
		}
	}

	return true;
}

bool Board::loadFromFile(char* a_boardFilename, char* a_spacesFilename, PropertyList* a_properties)
{
	if(!loadMap(a_boardFilename))
		return false;

	if(!loadSpaces(a_spacesFilename, a_properties))
		return false;

	return true;
}

// Draw a portion of the board starting at (a_x, a_y)
void Board::drawPortion(int a_x, int a_y)
{
	SetColor(BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE);

	for(int i = 0; i < VIEW_HEIGHT; i++)
	{
		gotoXY(m_x, m_y+i);
		
		for(int k = 0; k < VIEW_WIDTH; k++)
			printf("%c", m_map[a_y+i][a_x+k]);
	}

	SetColor(STD_COLOR);
}

void Board::drawSpace(int a_space)
{
	m_spaces[a_space].Draw();
}

int Board::getWidth() { return m_width; }
int Board::getHeight() { return m_height; }

int Board::getPosX() {	return m_x; }
int Board::getPosY() { return m_y; }

// Search for a space on the board, starting from a_start
int Board::searchSpace(char* a_search, int a_start)
{
	bool train = false;
	bool utility = false;

	// Searching for the nearest train station
	if(strncmp(a_search, "train", 10) == 0)
		train = true;

	// Searching for the nearest utility
	if(strncmp(a_search, "utility", 10) == 0)
		utility = true;

	for(int i = a_start; ; i++)
	{
		// Check for train stations
		if(train)
		{
			if(m_spaces[i].getColor() == TRAIN)
				return i;
		}

		// Check for utilities
		else if(utility)
		{
			if(m_spaces[i].getColor() == UTILITY)
				return i;
		}
		// Check regular spaces
		else if(strncmp(a_search, m_spaces[i].getName(), 20) == 0)
			return i;

		// Couldn't find anything? start from the beginning.
		if(i >=  m_spaces.size()-1)
			i = 0;	
	}

	return -1;
}

Space* Board::getSpace(int a_space){ return &m_spaces[a_space]; }
